package com.ttc.mBackup.BackupSMS;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ttc.mBackup.R;
import com.ttc.mBackup.BackupAgent.LoginDropbox;
import com.ttc.mBackup.BackupContacts.Contact;
import com.ttc.mBackup.BackupContacts.ContactDAO;
import com.ttc.mBackup.BackupContacts.StructuredName;


public class CustomDialog{
	private static final String ACCOUNT = "ACCOUNT";
    private static final String FILENAME = "FILENAME";
    private static final String METHOD = "METHOD";
    private static final String DATE = "DATE";
    
	private Context context;
	private String location;
	private ArrayList<String> phoneList;
	private String filename;
	private String date;
	
	/**
	 * 
	 * @param context
	 */
	public CustomDialog(Context context) {
		super();
		this.context = context;
	}
	/**
	 * 
	 * @param date
	 */
	public void setDate(String date) {
		this.date = date;
	}
	/**
	 * 
	 * @param context
	 * @param mode
	 * @param locationMode
	 * @param phoneList
	 * @param filename
	 */
	public CustomDialog(Context context, String locationMode,
			ArrayList<String> phoneList, String filename) {
		super();
		this.context = context;
		this.location = locationMode;
		this.phoneList = phoneList;
		this.filename = filename;
	}
	
	/**
	 * 
	 * @param context
	 * @param filename
	 */
	public CustomDialog(Context context, String filename) {
		super();
		this.context = context;
		this.filename = filename;
	}
	/**
	 * 
	 * @param view
	 * @param DialogString
	 */
	public void createDialogFileSelectable(final View view, String DialogString){
		
		if(DialogString.equals(DialogCode.STRING_BACKUP_NEW)){
		
			final EditText input = new EditText(context);
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setView(input);
			builder.setIcon(R.drawable.s_icon_json);
			builder.setTitle("Change File Name...");
			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int whichButton) {
			    	String temp = input.getText().toString().trim();
			    	if(!temp.equals("")){
			    		((TextView) view).setText(temp + ".jmb");
			    	}
			    	dialog.dismiss();
			    }
			});
			builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog dialog =  builder.create();
			dialog.show();
		}else if(DialogString.equals(DialogCode.STRING_BACKUP_INCREMENTAL)){
			
			List<String> listFile = null;
	    	listFile = new FileBrowser().getDir();
	    	final String[] list = new String[listFile.size()];
	    	listFile.toArray(list);
	    	
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			if(listFile.size() == 0){
				TextView text = new TextView(context);
				text.setText("No file here!");
				builder.setView(text);
				builder.setTitle("Error...");
				builder.setIcon(R.drawable.s_icon_error);
				builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}		
				});
			}else{
				builder.setSingleChoiceItems(list, 0, null);
				builder.setTitle("Choose File...");
				builder.setIcon(R.drawable.s_icon_json);
				builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						int itemChecked = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
						for (int i = 0; i < list.length; i++) {
							if (itemChecked == i) {
								((TextView) view).setText(list[i]);
							}
						}
					}
				});
				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
			}
			AlertDialog dialog = builder.create();
			dialog.show();
				
		}
	}
	
	/**
	 * 
	 * @param data
	 */
	public void createDialogComfirm(String msg, final String mode){
		TextView textview = new TextView(context);
		textview.setText(msg);
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Comfirm?");
		builder.setIcon(R.drawable.s_icon_question);
		builder.setView(textview);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				ProgressDialogPerform progressBar = new ProgressDialogPerform(context);
				if(mode.equals(DialogCode.STRING_BACKUP_NEW)){
					progressBar.setBackupMode(DialogCode.CODE_PROGRESS_BACKUP_NEW);
				}else if(mode.equals(DialogCode.STRING_BACKUP_INCREMENTAL)){
					progressBar.setBackupMode(DialogCode.CODE_PROGRESS_BACKUP_INCREMENTAL);
				}else if(mode.equals(DialogCode.STRING_RESTORE_OVERRIDE)){
					progressBar.setBackupMode(DialogCode.CODE_PROGRESS_RESTORE_OVERRIDE);
				}else if(mode.equals(DialogCode.STRING_RESTORE_INCREMENTAL)){
					progressBar.setBackupMode(DialogCode.CODE_PROGRESS_RESTORE_INCREMENTAL);
				}
				progressBar.setFilename(filename);
				if(location.equals("SD Card")){
					progressBar.setLocationCode(DialogCode.CODE_LOCATION_SD_CARD);
				}else if(location.equals("Dropbox")){
					progressBar.setLocationCode(DialogCode.CODE_LOCATION_DROPBOX);
				}else if(location.equals("Gmail")){
					progressBar.setLocationCode(DialogCode.CODE_LOCATION_GMAIL);
				}
				if(mode.equals(DialogCode.STRING_BACKUP_INCREMENTAL) 
						|| mode.equals(DialogCode.STRING_BACKUP_NEW)){
					storeInformation(date, filename, mode);
				}
				progressBar.setPhoneContact(phoneList);
				progressBar.onCreate();
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	/**
	 * 
	 * @param view
	 */
	public void createDialogLocationSelectable(final View view){
		String[] choice = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		choice = new String[]{"SD Card", "Dropbox", "Email"};
		builder.setTitle("Choose Location...");
		builder.setIcon(R.drawable.s_icon_file);
		final String[] temp = choice;
		
		builder.setSingleChoiceItems(choice, 0, null);
		
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				int itemChecked = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
				for (int i = 0; i < temp.length; i++) {
					if (itemChecked == i) {
						((TextView) view).setText(temp[i]);
						if(temp[i].equals("Dropbox")){
							Intent intent = new Intent(context, LoginDropbox.class);
							context.startActivity(intent);
						}
					}
				}
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	
	/**
	 * 
	 * @param view
	 * @return
	 */
	public ArrayList<String> createDialogPhoneSelectable(final View view){
		ArrayList<Contact> contact = new ContactDAO(context.getContentResolver()).readAllContacts();
		StructuredName name = new StructuredName();
		
		final String[] phoneName = new String[contact.size() + 1];
		phoneName[0] = "Check all";
		
		
		final ArrayList<String> phoneSelected = new ArrayList<String>();
		for (int i = 0; i < contact.size(); i++) {
			name = contact.get(i).getStructuredName();
				phoneName[i + 1] = name.getDisplayName();
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMultiChoiceItems(phoneName, null, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if (which == 0) {
					
					if(isChecked){
						for (int j = 0; j < phoneName.length; j++) {
							((AlertDialog) dialog).getListView().setItemChecked(j, true);
						}
					}else{
						for (int j = 0; j < phoneName.length; j++) {
							((AlertDialog) dialog).getListView().setItemChecked(j, false);
						}
					}
				}else if(which != 0 && !isChecked){
					((AlertDialog) dialog).getListView().setItemChecked(0, false);
				}
			}
		});
		builder.setTitle("Choose Contact...");
		builder.setIcon(R.drawable.s_icon_contact);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				long[] itemChecked = ((AlertDialog) dialog).getListView().getCheckItemIds();
				String checked = "";
				for (int i = 0; i < phoneName.length; i++) {
					for (int j = 0; j < itemChecked.length; j++) {
						if(itemChecked[j] == i){
							checked += phoneName[i] + ";";
							phoneSelected.add(j,phoneName[i]);
							Log.v("check", phoneSelected.get(j).toString());
						}
					}					
				}
				if(itemChecked.length == 0){
					((TextView) view).setText("Check all");
				}else{
					((TextView) view).setText(checked);
				}
				
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();	
		return phoneSelected;
	}
	
	/**
	 * 
	 * @param data
	 */
	public void createDialogResult(String data){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(data);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();		
	}
	/**
	 * 
	 * @param date
	 * @param filename
	 * @param method
	 */
	public void storeInformation(String date, String filename, String method){
		SharedPreferences account = context.getSharedPreferences(ACCOUNT, 0);
		
		SharedPreferences.Editor editor = account.edit();
		editor.putString(FILENAME, filename);
		editor.putString(DATE, date);
		editor.putString(METHOD, method);
		editor.commit();
	}
}
